package com.csx.stack;

import com.csx.array.Array;

/**
 *  数组实现栈
 * @author 陈胤训
 * <p>
 * create: 2019-04-01 10:50
 **/
public class ArrayStack<E> implements Stack<E> {

    Array<E> array;

    public ArrayStack(int capacity){
        array = new Array<>(capacity);
    }
    public ArrayStack(){
        array = new Array<>();
    }


    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public void push(E e) {
        array.addLast(e);
    }

    @Override
    public E pop() {
        return array.deleteLast();
    }

    @Override
    public E peek() {
        return array.getLast();
    }

    public int getCapacity(){
        return array.getCapacity();
    }

    /**
     * 重写toString方法
     * @return
     */
    @Override
    public String toString(){
        StringBuilder builder = new StringBuilder();
        builder.append("Stack: ");
        builder.append('{');
        for (int i = 0 ; i < array.getSize() ; i ++){
            builder.append(array.get(i));
            if ( i != array.getSize() - 1){
                builder.append(",");
            }
        }
        builder.append("} top");
        return builder.toString();
    }
}
